<?php


class SelectOrders  extends Connection{
	private $conn;

	public function __construct(){
		Connection::init();
		$this->conn = $this->getConnection();
	}

	public function countNotApprovedOrders(){
		$stmt = $this->conn->query("	SELECT COUNT( o.id )
											FROM  orders o
											JOIN order_status s ON ( o.order_status_id = s.id )
											JOIN shop sh ON (  o.shop_id = sh.id )
										WHERE s.name =  'изчаква' and sh.delivery_id < 99");

		$stmt->execute();
		$count = (int)$stmt->fetchColumn();;

		return $count;
	}
	public function extractNotApprovedOrders(){
		$arrayObj=array();
		$stmt = $this->conn->query("	SELECT o.id, o.date, o.info,o.shop_id, s.name as status
										FROM  orders o
										JOIN order_status s ON ( o.order_status_id = s.id )
										JOIN shop sh ON (  o.shop_id = sh.id )
										WHERE s.name =  'изчаква' and sh.delivery_id < 99");

		$stmt->execute();

		$array = $stmt->fetchall(PDO::FETCH_ASSOC);
		$n = count($array);
		for ($i=0 ; $i<$n ;$i++){
			$shopObj      =  new SelectShop();
			$array[$i]["shop"]=$shopObj->extractShopForNewOrders((int)$array[$i]["shop_id"]);

			$stocksObj      =  new SelectOrdersStock();
			$array[$i]["stocks"]= $stocksObj->extractStocksInOrder((int)$array[$i]["id"]);
		}

		foreach ( $array as $row) {
			$Obj = new Order($row);
			$arrayObj[]=$Obj;
		}

		return $arrayObj;
	}
	public function changeStatus($flag,$id) {
		$stmt = $this->conn->prepare("	UPDATE orders
										SET order_status_id =  ?
										WHERE id = ?");

		if ($flag) {
			$stmt->bindValue(1,2,PDO::PARAM_INT);
		} else {
			$stmt->bindValue(1,3,PDO::PARAM_INT);
		}
		$stmt->bindValue(2,(int)$id,PDO::PARAM_INT);
		$stmt->execute();
	}
}
?>